package cn.longwingstech.intelligence.agent.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.longwingstech.intelligence.agent.model.entity.ChatHistory;
import cn.longwingstech.intelligence.agent.model.vo.app.AppAndHistoryVO;
import cn.longwingstech.intelligence.agent.model.vo.app.AppVO;
import cn.longwingstech.intelligence.agent.model.vo.chathistory.ChatHistoryVO;
import cn.longwingstech.intelligence.agent.service.ChatHistoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.longwingstech.intelligence.agent.model.entity.App;
import cn.longwingstech.intelligence.agent.service.AppService;
import cn.longwingstech.intelligence.agent.mapper.AppMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author a1369
 * @description 针对表【app】的数据库操作Service实现
 * @createDate 2025-08-04 17:55:55
 */
@Service
public class AppServiceImpl extends ServiceImpl<AppMapper, App>
        implements AppService {
    @Resource
    private ChatHistoryService chatHistoryService;

    /**
     * 获取app和历史记录
     *
     * @param appid
     * @return
     */
    @Override
    public AppAndHistoryVO getAppAndHistory(Long appid) {
        App app = super.getOne(new LambdaQueryWrapper<App>().eq(App::getId, appid));
        Assert.notNull(app, "app不存在");
        List<ChatHistory> chatHistoryList = chatHistoryService.list(new LambdaQueryWrapper<ChatHistory>().eq(ChatHistory::getAppid, appid));
        Assert.notNull(chatHistoryList, "app历史记录不存在");
        // 转VO
        List<ChatHistoryVO> resultList = chatHistoryList.stream().map(t -> BeanUtil.toBean(t, ChatHistoryVO.class)).toList();
        // 设置进去
        AppAndHistoryVO result = BeanUtil.toBean(app, AppAndHistoryVO.class);
        result.setChatHistoryList(resultList);

        return result;
    }

    /**
     * 获取app和历史记录列表
     *
     * @return
     */
    @Override
    public List<AppAndHistoryVO> getAppAndHistoryList() {
        List<App> appList = super.list(new LambdaQueryWrapper<App>().eq(App::getUserId, StpUtil.getLoginIdAsLong()));
        Assert.notNull(appList, "app不存在");
        Assert.state(!appList.isEmpty(), "app不存在");
        List<Long> appIds = appList.stream().map(App::getId).toList();

        List<ChatHistory> chatHistoryList = chatHistoryService.list(
                new LambdaQueryWrapper<ChatHistory>().in(ChatHistory::getAppid, appIds));
        Assert.notNull(chatHistoryList, "app历史记录不存在");
        HashMap<Long, List<ChatHistory>> chatHistoryMap = new HashMap<>();
        chatHistoryList.forEach(t -> {
            List<ChatHistory> chatHistories = chatHistoryMap.computeIfAbsent(t.getAppid(), k -> new ArrayList<>());
            chatHistories.add(t);
        });

        return appList.stream().map(t -> {
            AppAndHistoryVO result = BeanUtil.toBean(t, AppAndHistoryVO.class);
            List<ChatHistory> chatHistories = chatHistoryMap.get(t.getId());
            List<ChatHistoryVO> list = chatHistories.stream().map(s -> BeanUtil.toBean(s, ChatHistoryVO.class)).toList();
            result.setChatHistoryList(list);
            return result;
        }).toList();
    }

    /**
     * 获取app列表
     *
     * @return
     */
    @Override
    public List<AppVO> getAppList() {
        List<App> list = super.list(new LambdaQueryWrapper<App>().eq(App::getUserId, StpUtil.getLoginIdAsLong()));
        return list.stream().map(t -> BeanUtil.toBean(t, AppVO.class)).toList();
    }
}




